package zdream.rockchronicle.core.input;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.controllers.Controller;
import com.badlogic.gdx.controllers.Controllers;
import com.badlogic.gdx.utils.Array;

import zdream.control.input.WorldInput;

/**
 * 配置键位
 * @author Zdream
 */
public class InputCenter {

	/**
	 * <p>键位映射部分
	 *
	 * 需要映射的键位是这些 (下面会罗列游戏中有意义的操作键, 以及默认映射的键盘键位以及 XBox 360 手柄键位 [未完成])
	 *
	 * <p>
	 * <li>MAP_START (PC: Z, ENTER, ESCAPE) : 启动、游戏内菜单
	 * <li>MAP_BACK (PC: X, TAB, BACKSPACE) : 状态栏菜单
	 * <li>MAP_ATTACK  (XBOX: X; PC: J) : 攻击 (白弹)
	 * <li>MAP_JUMP    (XBOX: A; PC: K, SPACE) : 跳跃
	 * <li>MAP_SPECIAL (XBOX: Y; PC: I) : 特殊武器攻击
	 * <li>MAP_STRONG  (XBOX: B; PC: H) : 重攻击, 洛克是白弹连发
	 * <li>1 ~ 8  (XBOX: 右摇杆; PC: 1~8) : 用于切换特殊武器
	 * <li>MAP_LB     (XBOX: LB; PC: Y) : 能力一
	 * <li>MAP_RUSH   (XBOX: RB; PC: U) : 能力二, 洛克是滑铲
	 * <li>MAP_LT     (XBOX: LT; PC: Q) : ???
	 * <li>MAP_RT     (XBOX: RT; PC: E) : ???
	 * <li>MAP_PROP1 (XBOX: 左十字左; PC: V) : 道具一
	 * <li>MAP_PROP2 (XBOX: 左十字上; PC: B) : 道具二
	 * <li>MAP_PROP3 (XBOX: 左十字右; PC: N) : 道具三
	 * <li>MAP_PROP4 (XBOX: 左十字下; PC: C) : 显示参数
	 * </li></p>
	 */
	public static final int
			MAP_UP = WorldInput.MAP_UP,
			MAP_DOWN = WorldInput.MAP_DOWN,
			MAP_LEFT = WorldInput.MAP_LEFT,
			MAP_RIGHT = WorldInput.MAP_RIGHT,
			MAP_WEAPON1 = WorldInput.MAP_WEAPON1,
			MAP_WEAPON2 = WorldInput.MAP_WEAPON2,
			MAP_WEAPON3 = WorldInput.MAP_WEAPON3,
			MAP_WEAPON4 = WorldInput.MAP_WEAPON4,
			MAP_WEAPON5 = WorldInput.MAP_WEAPON5,
			MAP_WEAPON6 = WorldInput.MAP_WEAPON6,
			MAP_WEAPON7 = WorldInput.MAP_WEAPON7,
			MAP_WEAPON8 = WorldInput.MAP_WEAPON8,
			MAP_BACK = WorldInput.MAP_BACK,
			MAP_START = WorldInput.MAP_START,
			MAP_ATTACK = WorldInput.MAP_ATTACK,
			MAP_JUMP = WorldInput.MAP_JUMP,
			MAP_SPECIAL = WorldInput.MAP_SPECIAL,
			MAP_STRONG = WorldInput.MAP_STRONG,
			MAP_LB = WorldInput.MAP_LB,
			MAP_RUSH = WorldInput.MAP_RUSH,
			MAP_LT = WorldInput.MAP_LT,
			MAP_RT = WorldInput.MAP_RT,
			MAP_PROP1 = WorldInput.MAP_PROP1,
			MAP_PROP2 = WorldInput.MAP_PROP2,
			MAP_PROP3 = WorldInput.MAP_PROP3,
			MAP_DETAIL = WorldInput.MAP_DETAIL;

	public static final int
			XBOX_A = 0,
			XBOX_B = 1,
			XBOX_X = 2,
			XBOX_Y = 3,
			XBOX_LB = 4,
			XBOX_RB = 5,
			XBOX_BACK = 6,
			XBOX_START = 7,
			XBOX_LT = 0x100, // 虚拟键位  是由 axis-4 来控制的, 值为正数
			XBOX_RT = 0x101; // 虚拟键位  是由 axis-4 来控制的, 值为负数
	
	public InputCenter() {
		p1 = new PlayerInput(PlayerInput.PLAYER_1P);
//		p2 = new PlayerInput((byte) 2);
	}
	
	public final PlayerInput p1/*, p2*/;
	
	/**
	 * 键盘键位的绑定
	 */
	public final Array<IKeyListener> keyListeners = new Array<>(IKeyListener.class);

	public void init() {
		Gdx.input.setInputProcessor(debuggerListener);
		
		// 控制手柄
		initController();
	}

	public static boolean Q_DEBUG = false;
	class InputListener implements InputProcessor {

		@Override
		public boolean keyDown(int keycode) {
			p1.keyPressed(keycode);
			if (keycode == 45) { // Q
				Q_DEBUG = true;
			}
			keyListeners.forEach(l -> l.onKeyPressed(keycode));
			return false;
		}

		@Override
		public boolean keyUp(int keycode) {
			p1.keyReleased(keycode);
//			p2.keyReleased(keycode);
			keyListeners.forEach(l -> l.onKeyReleased(keycode));
			return false;
		}

		@Override
		public boolean keyTyped(char character) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean touchDown(int screenX, int screenY, int pointer, int button) {
			// TODO 点击事件暂时不做
			return false;
		}

		@Override
		public boolean touchUp(int screenX, int screenY, int pointer, int button) {
			// TODO 点击事件暂时不做
			return false;
		}

		@Override
		public boolean touchDragged(int screenX, int screenY, int pointer) {
			// TODO 点击事件暂时不做
			return false;
		}

		@Override
		public boolean mouseMoved(int screenX, int screenY) {
			// TODO 鼠标移动事件暂时不做
			return false;
		}

		@Override
		public boolean scrolled(int amount) {
			// TODO 滚轮事件暂时不做
			return false;
		}
		
	}
	InputListener debuggerListener = new InputListener();
	
	/* **********
	 * 手柄部分 *
	 ********** */
	
	/**
	 * <p>初始手柄分配原则：
	 * <p>如果有手柄, 第一个分配给 p1, 第二个分配给 p2
	 * </p>
	 */
	private void initController() {
		Array<Controller> ctrls = Controllers.getControllers();
		if (ctrls.size != 0) {
			p1.bindController(ctrls.get(0));
		}
	}
}
